<template>
    <div class="big">
        <div class="navigation">
            <span @click="back">
                <img src="../../assets/home-img/返回1.png" alt="">
            </span>
            <h3>选择城市</h3>
            <span></span>
        </div>
        <main>
            <div class="mainTop">
                <span>
                    <nav>
                    </nav>
                    <p>当前位置：{{ city }}</p>
                    <RouterView />
                </span>
                <span class="refresh">
                    <img src="../../assets//home-img/刷新.png" alt="">
                    刷新
                </span>
            </div>
            <div class="mainMiddle">
                <div class="left">
                    <ul class="leftData">
                        <li v-for="(item, index) in addressList" :key="index" @click="handleClick(item, index)"
                            :class="index == active ? 'bg' : ''">
                            {{ item.province }}
                        </li>
                    </ul>
                </div>
                <div class="right">
                    <ul class="rightData">
                        <li v-for="(item, index) in citylist" :key="index" @click="feedback(item)">
                            {{ item }}
                        </li>
                    </ul>
                </div>
            </div>
        </main>
    </div>
</template>

<script setup>
import { RouterLink, RouterView } from "vue-router";
import { ref, onMounted } from 'vue';
import http from '../../http/http';
import { useRouter } from 'vue-router';
import axios from "axios";
const city = ref("");
axios
    .get("https://restapi.amap.com/v3/ip", {
        params: {
            key: "c7bcf91e14320fe1d740f42fa36664f6",
        },
    })
    .then(({ data }) => {
        console.log(data);
        city.value = data.city;
        localStorage.setItem('city',city.value)
    });
const router = useRouter();
const active = ref(0);
const citylist = ref([])
const addressList = ref([])

// 返回
const back = () => {
    // router.push('/home')
    history.back();
}

const handleClick = (item, index) => {
    citylist.value = item.cities
    active.value = index
}

const getAddressList = async () => {
    await http.get('/city').then(res => {
        console.log(res.data.data);
        addressList.value = res.data.data
    })
    citylist.value = addressList.value[0].cities
}

const feedback = (item) => {
    console.log(item);
    localStorage.setItem('addressItem', item)
    router.push('/home')
}


onMounted(() => {
    getAddressList();
})
</script>

<style lang="scss" scoped>
.big {
    width: 100%;
    height: 1332px;
    background-color: #fff;

    .navigation {
        width: 100%;
        height: 92px;
        display: flex;
        justify-content: space-around;
        align-items: center;

        h3 {
            flex: 1;
            text-align: center;
            font-size: 35px;
        }

        span {
            width: 40px;
            padding-top: 7px;

            img {
                width: 40px;
                height: 40px;
                margin: 0 10px;
            }
        }
    }

    main {
        width: 100%;
        height: 1240px;
        // margin: 0 auto;
        background-color: #f2f2f2;

        .mainTop {
            width: 100%;
            height: 80px;
            padding: 0 20px;
            // background-color: red;
            background-color: #fff;
            display: flex;
            justify-content: space-between;
            align-items: center;
            font-weight: 500;
            font-size: 28px;

            span {
                display: flex;
                align-items: center;

                img {
                    width: 30px;
                    height: 30px;
                }
            }

            .refresh {
                color: #0081ef;
            }
        }

        .mainMiddle {
            width: 100%;
            height: 92.8%;
            margin-top: 10px;
            background-color: #fff;
            display: flex;
            justify-content: space-between;

            .left {
                width: 50%;
                height: 100%;
                background-color: #f6f6f6;
                overflow: hidden;

                .leftData {
                    width: 100%;
                    height: 100%;
                    overflow: scroll;
                    -ms-overflow-style: none;

                    &::-webkit-scrollbar {
                        display: none;
                    }

                    li {
                        text-indent: 5px;
                        font-size: 28px;
                        line-height: 100px;
                    }
                }
            }

            .right {
                width: 50%;
                height: 100%;

                .rightData {
                    width: 100%;
                    height: 100%;
                    overflow: scroll;
                    -ms-overflow-style: none;

                    &::-webkit-scrollbar {
                        display: none;
                    }

                    li {
                        font-size: 28px;
                        text-indent: 20px;
                        line-height: 100px;
                    }
                }
            }
        }
    }
}

.bg {
    background-color: #fff;
    color: #2565e0;
}

.bg::before {
    content: "";
    width: 2px;
    margin-right: 25px;
    border-left: 7px solid #2565e0;
    height: 20px;
    background-color: #0081ef;
}
</style>
